//
//  MKPayKit.m
//  MKBaseLib
//
//  Created by cocoa on 15/3/27.
//  Copyright (c) 2015年 cocoa. All rights reserved.
//

#import "MKPayKit.h"
#import "WXApi.h"
#import <AlipaySDK/AlipaySDK.h>
#import "UPPayPlugin.h"
#import "UPPayPluginDelegate.h"

typedef void(^PayCompleteBlock)(MKPayResult result);

static NSString *weChatScheme = nil;
static NSString *alipayScheme = nil;

static PayCompleteBlock alipayBlock = nil;
static PayCompleteBlock weChatPayBlock = nil;
static PayCompleteBlock upPayBlock = nil;

static BOOL upPayTestModel = NO;


@interface MKPayKit () <UPPayPluginDelegate, WXApiDelegate>

@end


@implementation MKPayKit

+ (MKPayKit *)defaultInstance
{
    static MKPayKit *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^
                  {
                      instance = [[self alloc] init];
                  });
    return instance;
}

+ (BOOL)applicationOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSString *urlStr = url.absoluteString;
    
    if ([urlStr hasPrefix:[weChatScheme stringByAppendingString:@"://pay/"]])
    {
        return [WXApi handleOpenURL:url delegate:[MKPayKit defaultInstance]];
    }
    if ([urlStr hasPrefix:alipayScheme])
    {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url
                                                  standbyCallback:^(NSDictionary *resultDic)
        {
            [self dealWithAliapyResult:resultDic];
        }];
        return YES;
    }
    return NO;
}

#pragma mark - 支付宝
+ (void)updateAlipayScheme:(NSString *)scheme
{
    alipayScheme = scheme;
}

+ (void)alipayWithInfo:(NSString *)info complete:(PayCompleteBlock)complete
{
    NSAssert(alipayScheme != nil, @"没有设置支付宝scheme");
    alipayBlock = complete;
    [[AlipaySDK defaultService] payOrder:info fromScheme:alipayScheme callback:^(NSDictionary *resultDic)
     {
         [self dealWithAliapyResult:resultDic];
     }];
}

+ (void)dealWithAliapyResult:(NSDictionary *)resultDic
{
    MKPayResult res = MKPayResultFailed;
    if ([resultDic[@"resultStatus"] integerValue] == 9000)
    {
        res = MKPayResultSuccess;
    }
    else if ([resultDic[@"resultStatus"] integerValue] == 6001)
    {
        res = MKPayResultCancelled;
    }
    
    if (alipayBlock != nil)
    {
        alipayBlock(res);
    }
}

#pragma mark - 微信
+ (void)updateWeChatScheme:(NSString *)scheme
{
    weChatScheme = scheme;
}

+ (void)weChatPayWithReq:(PayReq *)req complete:(PayCompleteBlock)complete
{
    NSAssert(weChatScheme != nil, @"没有设置微信scheme");
    weChatPayBlock = complete;
    BOOL b = [WXApi sendReq:req];
    NSLog(@"%i", b);
}

- (void)onReq:(BaseReq *)req
{
    
}

- (void)onResp:(BaseResp *)resp
{
    MKPayResult res = MKPayResultFailed;
    if ([resp isKindOfClass:[PayResp class]])
    {
        switch (resp.errCode)
        {
            case WXSuccess:
                res = MKPayResultSuccess;
                break;
            case WXErrCodeUserCancel:
                res = MKPayResultCancelled;
                break;
            default:
                res = MKPayResultFailed;
                break;
        }
    }
    if (weChatPayBlock != nil)
    {
        weChatPayBlock(res);
    }
}

#pragma mark - 银联
/**
 @brief 启动银联测试模式
 */
+ (void)upPayTestMode
{
    upPayTestModel = YES;
}

+ (void)upPayWithTn:(NSString *)tn withController:(UIViewController *)vc complete:(PayCompleteBlock)complete
{
    upPayBlock = complete;
    [UPPayPlugin startPay:tn mode:upPayTestModel ? @"01" : @"00"
           viewController:vc delegate:[MKPayKit defaultInstance]];
}

- (void)UPPayPluginResult:(NSString *)result
{
    MKPayResult res = MKPayResultFailed;
    if ([result isEqualToString:@"success"])
    {
        res = MKPayResultSuccess;
    }
    else if ([result isEqualToString:@"cancel"])
    {
        res = MKPayResultCancelled;
    }
    else
    {
        res = MKPayResultFailed;
    }
    if (upPayBlock != nil)
    {
        upPayBlock(res);
        upPayBlock = nil;
    }
}

@end
